package com.wxit.gulimall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.wxit.gulimall.order.entity.OrderEntity;
import com.wxit.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxit.common.utils.PageUtils;
import com.wxit.common.utils.Query;

import com.wxit.gulimall.order.dao.OrderItemDao;
import com.wxit.gulimall.order.entity.OrderItemEntity;
import com.wxit.gulimall.order.service.OrderItemService;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMesage(Message message, OrderReturnReasonEntity content,Channel channel) throws InterruptedException {
        System.out.println("接收到消息----" + content);
        byte[] body = message.getBody();
        //消息头属性
        MessageProperties properties = message.getMessageProperties();
//        Thread.sleep(3000);
        System.out.println("消息处理完成:" + content.getName());
        //channel内按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("------------------" + deliveryTag);

        //签收货物，非批量模式
        try {
            if (deliveryTag % 2 == 0){
                //收货
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物------>" + deliveryTag);
            } else {
                //退货
                //long deliveryTag, boolean multiple, boolean requeue
                channel.basicNack(deliveryTag,false,true);
                //long deliveryTag, boolean requeue
//                channel.basicReject();
                System.out.println("没有签收货物------>" + deliveryTag);
            }
        } catch (Exception e) {
            //网络终端
            e.printStackTrace();
        }
        //
//        channel.basicAck();
    }


    @RabbitHandler
    public void recieveMesage2(OrderEntity content) throws InterruptedException {

        System.out.println("接收到的消息内容:" + content);
    }

}
